﻿using SelElectCalcModernUI.ViewModel.CalcRes;
using SelElectCalcModernUI.ViewModel.Settings;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace SelElectCalcModernUI.ViewModel
{
    /// <summary>
    /// 视图模型管理器
    /// </summary>
    public class ViewModelLocator
    {
        //外观设计配置视图模型
        static AppearanceViewModel _appearance;
        //主输入视图模型
        static HomeViewModel _home;
        //展示买方计算结果视图模型
        static SellerResultViewModel _sellerResult;
        //展示卖方计算结果视图模型
        static FactoryResultViewModel _factoryResult;
        //展示交易结果
        static AllDealViewModel _allDeal;
        //用于其他设置
        static OtherSettingViewModel _otherSetting;
        //用于弹出消息窗口的事件
        public static event Action<string> MessageShow;
        //用于弹出自定义窗口的事件
        public static event Func<string, bool> DialogShow;
        //用于跳转页面的事件
        public static event Action<string> JumpPage;
        //用于保存文件的事件
        public static event Func<string, Stream> SaveFile;
        /// <summary>
        /// 初始化。为了数据能够全部从配置文件中获取，这里全部视图模型初始化
        /// </summary>
        public ViewModelLocator()
        {
            if (_appearance == null)
                _appearance = new AppearanceViewModel();
            if (_otherSetting == null)
                _otherSetting = OtherSettingViewModel.CreateOtherSettingViewModel();
            if (_home == null)
                _home = new HomeViewModel();
        }
        /// <summary>
        /// 触发弹出窗口的事件，由视图模型调用
        /// </summary>
        /// <param name="message">弹出窗口展示的消息</param>
        public static void SendMessageEvent(string message)
        {
            if (MessageShow != null)
                MessageShow(message);
        }
        public static bool DialogShowEvent(string page)
        {
            if (DialogShow != null)
                return DialogShow(page);
            else
                return false;
        }
        /// <summary>
        /// 触发跳转页面的事件，由视图模型调用
        /// </summary>
        /// <param name="page">目标页面</param>
        public static void JumpPageEvent(string page)
        {
            if (JumpPage != null)
                JumpPage(page);
        }
        /// <summary>
        /// 触发跳转页面的事件，由视图模型调用
        /// </summary>
        /// <param name="page">目标页面</param>
        public static Stream SaveFileEvent(string ext)
        {
            if (SaveFile != null)
                return SaveFile(ext);
            else
                return null;
        }
        /// <summary>
        /// 主输入视图模型
        /// </summary>
        public static HomeViewModel Home
        {
            get
            {
                if (_home == null)
                    _home = new HomeViewModel();
                return _home;
            }
        }
        /// <summary>
        /// 用于外观设计配置的视图模型
        /// </summary>
        public static AppearanceViewModel Appearance
        {
            get
            {
                if (_appearance == null)
                    _appearance = new AppearanceViewModel();
                return _appearance;
            }
        }
        /// <summary>
        /// 用于展示买方计算结果的视图模型
        /// </summary>
        public static SellerResultViewModel SellerResult
        {
            get
            {
                if (_sellerResult == null)
                    _sellerResult = new SellerResultViewModel();
                return _sellerResult;
            }
        }
        /// <summary>
        /// 用于展示卖计算结果的视图模型
        /// </summary>
        public static FactoryResultViewModel FactoryResult
        {
            get
            {
                if (_factoryResult == null)
                    _factoryResult = new FactoryResultViewModel();
                return _factoryResult;
            }
        }
        /// <summary>
        /// 用于其他设置的视图模型
        /// </summary>
        public static OtherSettingViewModel OtherSetting
        {
            get
            {
                if (_otherSetting == null)
                    _otherSetting = new OtherSettingViewModel();
                return _otherSetting;
            }
        }

        public static AllDealViewModel AllDeal
        {
            get
            {
                if (_allDeal == null)
                    _allDeal = new AllDealViewModel();
                return _allDeal;
            }

            set
            {
                _allDeal = value;
            }
        }

        /// <summary>
        /// 退出时清理资源
        /// </summary>
        public static void Cleanup()
        {
            Cleanup(_home);
            Cleanup(_appearance);
            Cleanup(_sellerResult);
            Cleanup(_factoryResult);
            Cleanup(_otherSetting);
            Cleanup(_allDeal);
        }
        /// <summary>
        /// 如果对象为可释放资源的类型（如退出时保存，关闭硬件资源或文件资源），则进行资源释放
        /// </summary>
        /// <param name="obj">需要释放资源的对象</param>
        private static void Cleanup(object obj)
        {
            if (obj is IDisposable)
                ((IDisposable)obj).Dispose();
        }
    }
}
